package gl.android.biyiqing.fragment;

import gl.android.biyiqing.FragmentContainerActivity;
import gl.android.biyiqing.R;
import gl.android.biyiqing.api.API;
import gl.android.biyiqing.competent.PayView;
import gl.common.AjaxCallBackProxy;
import gl.common.BaseControl;
import gl.common.UserManger;
import gl.uitls.AndroidUtil;
import gl.uitls.Tip;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

public class ShopFragment extends BaseFragment implements
		android.view.View.OnClickListener {

	final String BUY = "购买";
	// Content View Elements
	final int price_post = 2;// 金币打折算法
	private View jinbi;
	private View post;

	// End Of Content View Elements

	private void bindViews() {

		jinbi = (View) rootView.findViewById(R.id.pay);
		post = (View) rootView.findViewById(R.id.btn_buy);
		jinbi.setOnClickListener(this);
		post.setOnClickListener(this);
		findViewById(R.id.btn_buy_xk1)
				.setOnClickListener(
						new BuyOnClickIMP(
								"BuySMSMonthly?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										49, 30 }));
		findViewById(R.id.btn_buy_xk2)
				.setOnClickListener(
						new BuyOnClickIMP(
								"BuySMSMonthly?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										79, 90 }));
		findViewById(R.id.btn_buy_xk3)
				.setOnClickListener(
						new BuyOnClickIMP(
								"BuySMSMonthly?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										99, 180 }));
		findViewById(R.id.btn_buy_xk4).setOnClickListener(
				new BuyOnClickIMP("BuySMSMonthly?userid=%s&am=%s&days=%s",
						new Object[] { UserManger.getInstance().userid, 129,
								365 }));

		findViewById(R.id.btn_buy_sy1)
				.setOnClickListener(
						new BuyOnClickIMP("BuyDisplay?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										20, 1 }));
		findViewById(R.id.btn_buy_sy2)
				.setOnClickListener(
						new BuyOnClickIMP("BuyDisplay?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										600, 30 }));

		findViewById(R.id.btn_buy_ss1)
				.setOnClickListener(
						new BuyOnClickIMP("BuyRank?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										10, 1 }));
		findViewById(R.id.btn_buy_ss2)
				.setOnClickListener(
						new BuyOnClickIMP("BuyRank?userid=%s&am=%s&days=%s",
								new Object[] { UserManger.getInstance().userid,
										199, 30 }));

		findViewById(R.id.hy1).setOnClickListener(this);
		findViewById(R.id.hy2).setOnClickListener(this);
		findViewById(R.id.hy3).setOnClickListener(this);
	}

	class BuyOnClickIMP implements View.OnClickListener {
		private Object[] pars;
		private String url;

		BuyOnClickIMP(String url, Object[] pars) {
			this.url = url;
			this.pars = pars;
		}

		@Override
		public void onClick(View v) {
			buy(url, pars);
		}
	}

	public static ShopFragment newInstance() {
		ShopFragment fragment = new ShopFragment();
		Bundle args = new Bundle();
		fragment.setArguments(args);
		return fragment;
	}

	public ShopFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_shop, container, false);
		bindViews();
		initData();
		return rootView;
	}

	private void initData() {
		initCountInfo();
	}

	String ERR = "购买失败";

	public void buy(String url, Object[] pars) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {

			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					handErrMsg(t);
					final JSONObject jsonObject = new JSONObject(t);
					if ("buymoney".equals(jsonObject.getString("act"))
							&& "2".equals(jsonObject.getString("msg"))) {
						Tip.show("购买金币成功");
						initCountInfo();
					}
					else if ("buypost".equals(jsonObject.getString("act"))
							&& "3".equals(jsonObject.getString("msg"))) {
						Tip.show("购买邮票成功");
						initCountInfo();
					}
					else if ("buypost".equals(jsonObject.getString("act"))
							&& "0".equals(jsonObject.getString("msg"))) {
						Tip.show("金币不足");
					}
					else if ("3".equals(jsonObject.getString("msg"))) {
						Tip.show("购买成功");
						initCountInfo();
					}
					else if ("0".equals(jsonObject.getString("msg"))) {
						Tip.show("金币不足");
					}
				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show(ERR);
			}
		}, pars);
	}

	public void initCountInfo() {
		// getCount("GetPostCount?userid=%s", post, "拥有邮票:%s");
		// getCount("GetMoneyCount?userid=%s", jinbi, "拥有金币:%s");
	}

	void getCount(String url, final TextView tv, final String format) {
		BaseControl.get(API.URL + url, new AjaxCallBackProxy() {
			@Override
			public void onSuccess(String t) {
				super.onSuccess(t);
				try {
					tv.setText(String.format(format,
							new JSONObject(t).getString("data")));

				} catch (Exception e) {
					e.printStackTrace();
					onFailure(e, 0, e.getMessage());
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				Tip.show("获取拥有财产数量失败");
			}
		}, new Object[] { UserManger.getInstance().userid });
	}

	private void handErrMsg(String t) {
	}

	AlertDialog _Dialog;

	public void onClickBuyJINBI(final String url) {
		final EditText et = new EditText(getActivity());

		et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);

		View PayView = new PayView(getActivity(), new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				_Dialog.dismiss();
				try {

					String[] pars = new String[] {
							UserManger.getInstance().userid,
							v.getTag().toString() };
//					buy(url, pars);//支付宝购买
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}).getView();
		View PayView1 = new PayView(getActivity(), new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				_Dialog.dismiss();
				try {
					
					String[] pars = new String[] {
							UserManger.getInstance().userid,
							v.getTag().toString() };
//					buy(url, pars);//支付宝购买
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		}).getView();
		_Dialog = AndroidUtil.genCustomViewDialog(getActivity(), PayView)
				.show();
		Window window = _Dialog.getWindow();
		window.setContentView(PayView1);
		_Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
		_Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
	}

	public void onClickBuyPost(final String url) {
		final EditText et = new EditText(getActivity());
		et.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
		AndroidUtil.genCustomViewDialog(getActivity(), et, "请输入数量", BUY,
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						int am = 0;
						try {
							am = (Integer.parseInt(et.getText().toString()));
							if (et.getText() != null && am > 0) {
								int qu = am;

								String[] pars = new String[] {
										UserManger.getInstance().userid,
										String.valueOf(am * price_post),
										String.valueOf(qu) };
								buy(url, pars);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}

					}
				});
	}

	@Override
	public void onClick(View v) {
		String url;
		if (v == jinbi) {
			url = "BuyMoney?userid=%s&am=%s";
			onClickBuyJINBI(url);
		}
		else if (v == post) {
			url = "BuyPost?userid=%s&am=%s&qu=%s";
			onClickBuyPost(url);
		}
		else if (v == findViewById(R.id.hy1)) {
			Bundle bundle = new Bundle();
			bundle.putString(BaseFragment.KEY_SEX, "1");
			final String basefragmentclassname = FragmentContainerActivity.BaseFragmentClassName;
			startFragmentActivity(bundle, basefragmentclassname);
		}
		else if (v == findViewById(R.id.hy2)) {
			Bundle bundle = new Bundle();
			bundle.putString(BaseFragment.KEY_SEX, "2");
			final String basefragmentclassname = FragmentContainerActivity.BaseFragmentClassName;
			startFragmentActivity(bundle, basefragmentclassname);
//			Tip.show("暂未推出");
		}
		else if (v == findViewById(R.id.hy3)) {
			Bundle bundle = new Bundle();
			bundle.putString(BaseFragment.KEY_SEX, "3");
			final String basefragmentclassname = FragmentContainerActivity.BaseFragmentClassName;
			startFragmentActivity(bundle, basefragmentclassname);
		}

	}

}
